package com.example.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

/**
 * 本类中保存参数检测方法
 */
public class CheckForStyle {
    private Logger logger = LoggerFactory.getLogger(getClass());

    public CheckForStyle() {
    }

    /*
    判断参数是否为空
     */
    public Boolean isNull(String str) {
        if (str == null || str.equals("") || str.equals("string")) {
            return true;
        }
        return false;
    }

    public Boolean isNull(int num) {
        if (num == 0) {
            return true;
        }
        return false;
    }

    public Boolean isNull(Integer num) {
        if (num == null) {
            return true;
        }
        return false;
    }

    public Boolean isNull(Date time) {
        if (time == null) {
            return true;
        }
        return false;
    }

    public Boolean isNull(BigDecimal num) {
        if (num == null) {
            return true;
        }
        /*
        if (num.equals(BigDecimal.ZERO)) {
            return true;
        }
        */
        return false;
    }

    public Boolean isNull(double num) {
        if (num == 0) {
            return true;
        }
        return false;
    }

    public Boolean isNull(Double num) {
        if (num == null || num == 0) {
            return true;
        }
        return false;
    }

    public Boolean isNull(float num) {
        if (num == 0) {
            return true;
        }
        return false;
    }

    public Boolean isNull(Float num) {
        if (num == null || num == 0) {
            return true;
        }
        return false;
    }


    /**
     * 检测必要参数，不符合格式的话，则抛出异常
     *
     * @param num 参数
     * @param msg 报警信息
     * @param val 异常值
     */
    public Integer checkNessisaryParmas(Integer nessaryCount, int num, String msg, Integer val, String classname) {
        if (isNull(num)) {
            System.out.println(msg);
            logger.error(msg, classname);
            throw new BaseException(msg, val);
        }
        return nessaryCount + 1;
    }

    public Integer checkNessisaryParmas(Integer nessaryCount, Integer num, String msg, Integer val, String classname) {
        if (isNull(num)) {
            System.out.println(msg);
            logger.error(msg, classname);
            throw new BaseException(msg, val);
        }
        return nessaryCount + 1;
    }

    public Integer checkNessisaryParmas(Integer nessaryCount, BigDecimal num, String msg, Integer val, String classname) {
        if (isNull(num)) {
            System.out.println(msg);
            logger.error(msg, classname);
            throw new BaseException(msg, val);
        }
        return nessaryCount + 1;
    }

    public Integer checkNessisaryParmas(Integer nessaryCount, Double num, String msg, Integer val, String classname) {
        if (isNull(num)) {
            System.out.println(msg);
            logger.error(msg, classname);
            throw new BaseException(msg, val);
        }
        return nessaryCount + 1;
    }

    public Integer checkNessisaryParmas(Integer nessaryCount, String str, String msg, Integer val, String classname) {
        if (isNull(str)) {
            System.out.println(msg + classname);
            logger.error(msg, classname);
            throw new BaseException(msg, val);
        }
        return nessaryCount + 1;
    }

    public void checkNessisaryParmas(Date day, String msg, int val, String classname) {
        if (isNull(day)) {
            System.out.println(msg + classname);
            logger.error(msg, classname);
            throw new BaseException(msg, val);
        }
    }

    public Integer checkNessisaryParmas(Integer nessaryCount, Date day, String msg, Integer val, String classname) {
        if (isNull(day)) {
            System.out.println(msg + classname);
            logger.error(msg, classname);
            throw new BaseException(msg, val);
        }
        return nessaryCount + 1;
    }

    /**
     * 输出标准报错样式
     */
    public void sendError(String msg, String className, Integer errorCode) {
        logger.error(msg, className);
        throw new BaseException(msg, errorCode);
    }

    /**
     * 输出标准报错样式
     */
    public void sendError(String error, String track, String msg, String className, Integer errorCode) {
        System.out.printf(error);
        System.out.printf(track);
        logger.error(msg, className);
        throw new BaseException(msg, errorCode);
    }


    /**
     * 用于bigdecimal的数据的为空取默认值
     *
     * @param val
     * @return
     */
    public BigDecimal ifnullForZero(BigDecimal val) {
        return (val == null) ? BigDecimal.ZERO : val;
    }

    public Integer ifnullForZero(Integer val) {
        return (val == null) ? 0 : val;
    }

    /**
     * 获取指定时间所在月份的第一天时间并返回
     *
     * @param time
     * @return
     */
    public Date getMonthFirstDay(Date time) {
        Calendar cale = Calendar.getInstance();
        cale.setTime(time);
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        return cale.getTime();
    }

    /**
     * 获取指定时间所在月份的最后一天时间并返回
     *
     * @param time
     * @return
     */
    public Date getMonthLastDay(Date time) {
        Calendar cale = Calendar.getInstance();
        cale.setTime(time);
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        cale.add(Calendar.DATE, -1);
        return cale.getTime();
    }


}
